<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="Titles" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form ref="eref" label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="clickAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id" default-expand-all>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- {{ row }} -->
              <el-button v-if="row.type===1" size="small" type="text" @click="clickAdd(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="clickShowEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="clickDel(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqUpdatePermission, reqGetPermissionDetail, reqDelPermission } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {

  name: 'Permission',
  data() {
    return {
      showDialog: false,
      list: [],
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  },
  // 计算属性控制标题
  computed: {
    Titles() {
      return this.formData.id ? '查看编辑' : '添加权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      // console.log(data)
      this.list = listToTreeData(data, '0')
    },
    // 打开模态框
    // type指的是type是1添加的页面访问权 2是按钮访问权
    // pid 父亲id
    clickAdd(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    // 关闭模态框
    closeDialog() {
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
      this.showDialog = false
    },
    // 点击确认，发送请求
    async clickSubmit() {
      if (this.formData.name === '' || this.formData.code === '') {
        return this.$message.success('请完善表单')
      }
      if (this.formData.id) {
        // 编辑操作
        await reqUpdatePermission(this.formData)
        this.$message.success('修改权限成功')
      } else {
        // 添加操作
        await reqAddPermission(this.formData)
        this.$message.success('添加权限成功')
      }
      this.getPermissionList()
      this.closeDialog()
      // console.log(res)
    },
    // 点击删除当前权限
    clickDel(row) {
      this.$confirm(`此操作将永久删除${row.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await reqDelPermission(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 更新页面
        this.getPermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 查看功能
    async clickShowEdit(id) {
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
      this.showDialog = true
    }

  }
}
</script>

  <style>

  </style>
